package styledaco.board;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import statics.SoptVars;
import styledaco.member.MemberProc;

import com.ibatis.sqlmap.client.SqlMapClient;

import comm.util.CommUtil;
import comm.util.HTMLInputFilter;
import comm.util.RequestManager;
import comm.util.SoptHashMap;
import comm.util.SqlMapLoader;

public class BoardProc {
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	private Logger logger = Logger.getLogger(MemberProc.class);
	
	public String regBoard(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
		String result = SoptVars.FAIL;
		String boardSeq = "";
		try {
			HTMLInputFilter filter = new HTMLInputFilter(false);
			hm.put("BOARD_CONTENT", filter.filter(hm.get("BOARD_CONTENT").toString()));
			result = boardSeq = CommUtil.NVL(sqlMapper.insert("board.regBoard",hm),"");
			if(hm.get("PARENT")!=null && hm.get("PARENT").toString().length()>0){
				SoptHashMap parentHm = new SoptHashMap();
				parentHm.put("BOARD_SEQ", result);
				parentHm.put("PARENT_SEQ", hm.get("PARENT"));
				result = CommUtil.NVL(sqlMapper.update("board.updateBoardParent",parentHm),"");
			}else{
				result = CommUtil.NVL(sqlMapper.update("board.updateBoardParentSelf",result),"");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(result.length()>0 && boardSeq.length()>0)return boardSeq;
		else return "";
	}
	
	public String updateBoard(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
		String result = SoptVars.FAIL;
		try {
			
			HTMLInputFilter filter = new HTMLInputFilter(false);
			hm.put("BOARD_CONTENT", filter.filter(hm.get("BOARD_CONTENT").toString()));
			result = CommUtil.NVL(sqlMapper.update("board.updateBoard", hm),"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public String deleteBoard(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
		String result = SoptVars.FAIL;
		try {
			result = CommUtil.NVL(sqlMapper.update("board.deleteBoard", hm),"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	public SoptHashMap getBoard(String seq){
		SoptHashMap hm = null;
		try {
			hm = (SoptHashMap)sqlMapper.queryForObject("board.getBoardBySeq",seq);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hm;
	}
	
	public ArrayList getBoardList(String category,int curPage,int pageSize){
		ArrayList lt = null;
		
		try{
			if(category.length()>0){
				SoptHashMap hm = new SoptHashMap();
				hm.put("CATEGORY_SEQ",category);
				hm.put("PAGE_SIZE",pageSize<1?SoptVars.PAGE_SIZE:pageSize);
				hm.put("PAGE",(curPage-1)*(pageSize<1?SoptVars.PAGE_SIZE:pageSize));
				
				lt = (ArrayList) sqlMapper.queryForList("board.getBoardListByCategory",hm);
			}else{
				lt = (ArrayList) sqlMapper.queryForList("board.getBoardTotalList");
			}
		}catch (Exception e) {
			return lt;
		}
		
		return lt;
	}
	
	public ArrayList getBoardListByType(String type,int curPage,int pageSize){
		ArrayList lt = null;
		
		try {
			SoptHashMap hm = new SoptHashMap();
			hm.put("TYPE",type);
			hm.put("PAGE_SIZE",pageSize<1?SoptVars.PAGE_SIZE:pageSize);
			hm.put("PAGE",(curPage-1)*(pageSize<1?SoptVars.PAGE_SIZE:pageSize));
			lt = (ArrayList)sqlMapper.queryForList("board.getBoardListByType",hm);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}
	
	public ArrayList getBoardSearchListByTitle(String q){
		ArrayList lt = null;
		
		try {
			lt = (ArrayList) sqlMapper.queryForList("board.getBoardSearchListByTitle",q);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}
	
	public ArrayList getBoardSearchListByContent(String q){
		ArrayList lt = null;
		
		try {
			lt = (ArrayList) sqlMapper.queryForList("board.getBoardSearchListByContent",q);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lt;
	}
}
